package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.OverflowListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.service.OverflowListGoodsService;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lfh
 * @version 1.0
 * @date 2021/5/15 17:37
 */
@Service
public class OverflowListGoodsServiceImpl implements OverflowListGoodsService {

    @Autowired
    private OverflowListGoodsDao overflowListGoodsDao;

    // 新增报溢单
    @Override
    public ServiceVO save(OverflowList overflowList, String overflowListGoodsStr, String overflowNumber) {
        try {
            // 新增报溢单
            overflowListGoodsDao.save(overflowList);
            // 创建一个gson对象
            Gson gson = new Gson();
            // 把json格式的字符串转换成List
            List<OverflowListGoods> overflowListGoods = gson.fromJson(overflowListGoodsStr, new TypeToken<List<OverflowListGoods>>(){}.getType());
            overflowListGoodsDao.saveOverflowListGoods(overflowListGoods);
        } catch (JsonSyntaxException e) {
            return new ServiceVO(400, "保存失败！");
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> getOverflowList(String sTime, String eTime) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowList> overflowLists =  overflowListGoodsDao.getOverflowList(sTime,eTime);
        map.put("rows", overflowLists);
        return map;
    }

    // 报溢单商品信息
    @Override
    public Map<String, Object> getvOerflowListGoods(Integer overflowListId) {
        HashMap<String, Object> map = new HashMap<>();
        List<OverflowListGoods> oerflowListGoods =  overflowListGoodsDao.getOerflowListGoods(overflowListId);
        map.put("rows", oerflowListGoods);
        return map;
    }
}
